package st.business.tepth.business.visitor;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

import java.lang.ref.WeakReference;
import java.util.List;

import st.business.tepth.business.bean.LocationBean;
import st.business.tepth.business.visitor.busnumber.BusNumberDelegate;

/**
 * @author zhangyu
 * @date 2019/1/17
 */
public class StationAdapter extends FragmentStatePagerAdapter {
    private SparseArray<WeakReference<Fragment>> registeredFragments =
            new SparseArray<WeakReference<Fragment>>();
    private List<LocationBean.FactoryAreaListBean> mList;

    private StationAdapter(FragmentManager fm) {
        super(fm);
    }

    public StationAdapter(FragmentManager fm, List<LocationBean.FactoryAreaListBean> list) {
        this(fm);
        mList = list;
    }

    @Override
    public Fragment getItem(int position) {
        return BusNumberDelegate.newInstance(mList.get(position)
                .getId());
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        // TODO Auto-generated method stub
        // 得到缓存的fragment
        BusNumberDelegate fragment = (BusNumberDelegate) super.instantiateItem(container,
                position);
        WeakReference<Fragment> weak = new WeakReference<Fragment>(fragment);
        registeredFragments.put(position, weak);
        return fragment;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position,
                            @NonNull Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);

    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mList.get(position).getName();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }


}
